<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>优惠券管理</title>
    <link href="/resources/hotel/css/style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        input{
            border: #00a4ac 0.3px solid;
        }

        .couponUL li{
            font-size: 16px;
            margin: 15px 0px;
        }
        .tablelist input{
            height: 30px;
            border: #00a4ac solid 1px;
        }
        #addUlTh li{
            font-size: 15px;
            margin-top: 10px;
            text-align: right;
        }
        #addUlTd li{
            margin-top: 10px;
        }
        #addUlTd li input{
            height: 20px;
        }
        .paginList li{
            margin-left: 50px;
        }

        #modifyUlTh li{
            font-size: 15px;
            margin-top: 10px;
            text-align: right;
        }
        #modifyUlTd li{
            margin-top: 10px;
        }
        #modifyUlTd li input{
            height: 20px;
        }
    </style>
    <script type="text/javascript" src="/resources/hotel/js/jquery.js"></script>
    <script type="text/javascript" src="/resources/hotel/js/util.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $(".click").click(function(){
                $(".tip").fadeIn(200);
            });

            $(".tiptop a").click(function(){
                $(".tip").fadeOut(200);
            });

            $(".sure").click(function(){
                $(".tip").fadeOut(100);
            });

            $(".cancel").click(function(){
                $(".tip").fadeOut(100);
            });

        });
    </script>


</head>


<body>
<div>
    <div class="place">
        <span>位置：</span>
        <ul class="placeul">
            <li><a href="#">酒店信息管理</a></li>
            <li><a href="#">优惠券管理</a></li>
        </ul>
    </div>

    <div class="rightinfo">
        <!--添加按钮-->
        <div class="tools">
            <ul class="toolbar">
                <li id="addButton"><span><img src="/resources/hotel/images/t01.png" /></span>添加</li>
            </ul>
        </div>
        <!--添加框-->
        <form id="addForm">
            <div class="tip" style="height: 450px; display: none;" id="addDiv">
                <div class="tiptop"><span>添加优惠券信息</span><a></a></div>
                <div style="margin-left: 10px;">
                    <div style="float: left;">
                        <ul id="addUlTh">
                            <li>优惠券名称</li>
                            <li>优惠券描述</li>
                            <li style="margin-top: 43px;">最低消费额</li>
                            <li>优惠券面值</li>
                            <li>优惠券的数量</li>
                            <li>优惠券生效时间</li>
                            <li>优惠券失效时间</li>
                        </ul>
                    </div>
                    <div style="float: left;margin-left: 10px;">
                        <ul id="addUlTd">
                            <li><input type="text" onkeyup="checkLength(this,20)" placeholder="最多10个字" id="addInputDName" name="dName"></li>
                            <li>
                                <textarea style="border: #00a4ac solid 0.2px;height: 50px;" onkeyup="checkLength(this,50)" placeholder="最多25字" id="addInputDDesc" name="dDesc"></textarea>
                            </li>
                            <li><input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')" id="addInputDMinConsum" name="dMinConsum"></li>
                            <li><input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')" id="addInputDValue" name="dValue"></li>
                            <li><input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" id="addInputDCount" name="dCount"></li>
                            <li><input type="date" id="addInputDStart" name="dStart"></li>
                            <li><input type="date" id="addInputDStop" name="dStop"></li>
                        </ul>
                    </div>
                </div>
                <div class="tipbtn" style="position: absolute;top:-90px;">
                    <input name="" type="button"  class="sure" value="确定添加" id="addOK"/>&nbsp;
                    <input name="" type="button"  class="cancel" value="取消" />
                </div>
            </div>
        </form>

        <!--搜索框部分-->
        <div style="margin-top:10px;">
            <div class="formtitle"><span>搜索优惠券</span></div>
            <div>
                <table class="tablelist" >
                    <thead>
                    <tr>
                        <th style="width:250px;">根据优惠额度查询</th>
                        <th style="width:250px;">根据消费金额查询</th>
                        <th>优惠券生效时间</th>
                        <th>优惠券失效时间</th>
                        <th>优惠券状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td><input type="text" placeholder="请输入优惠券的面值" style="width: 120px;height: 30px;"></td>
                        <td><input type="text" placeholder="请输入消费金额" style="width: 120px;height: 30px;"></td>
                        <td><input type="date"></td>
                        <td><input type="date"></td>
                        <td>
                            <select style="height: 30px;">
                                <option value="-1">---请选择优惠券状态---</option>
                                <option value="0">已失效</option>
                                <option value="1">审核中</option>
                                <option value="2">已生效</option>
                            </select>
                        </td>
                        <td><a href="#" class="tablelink">搜索</a></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div style="margin-top:10px;">
            <table class="tablelist" >
                <thead>
                <tr>
                    <th>名称</th>
                    <th>描述</th>
                    <th>优惠券面值</th>
                    <th>创建者名称</th>
                    <th>创建时间</th>
                    <th>生效时间</th>
                    <th>失效时间</th>
                    <th>最低消费额</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="hadCouponBody">
                </tbody>
            </table>

            <!--翻页的div-->
            <div class="pagin">
                <div class="message">共 <i class="blue" id="countData">0</i> 条记录，共 <i class="blue" id="countPage">0</i> 页，当前显示第&nbsp;<i class="blue" id="nowPage">0&nbsp;</i>页</div>
                <ul class="paginList" style="margin-right: 50px;">
                    <li class="paginItem"><span><a  style="width: 70px;" id="upPage" onclick="flip(-1)">上一页</a></span></li>
                    <li class="paginItem">
                        <input id="finPageBox" type="text" style="width: 60px;height: 25px; " onkeypress="return event.keyCode>=48&&event.keyCode<=57" ng-pattern="/[^a-zA-Z]/"/>
                        <button style="padding: 5px 10px;font-size: 12px;" id="findPageButton" onclick="flip(0)">跳转</button>
                    </li>
                    <li class="paginItem"><span><a style="width: 70px;border: solid 1px lavender;" id="nextPage" onclick="flip(1)">下一页</a></span></li>
                </ul>
            </div>

        </div>
    </div>

    </div>
</div>

<!--修改框div-->
<form id="modifyForm">
    <div class="tip" style="height: 450px; display: none;" id="modifyDiv">
        <div class="tiptop"><span>修改优惠券信息</span><a></a></div>
        <div style="margin-left: 10px;">
            <div style="float: left;">
                <ul id="modifyUlTh">
                    <li>优惠券名称</li>
                    <li>优惠券描述</li>
                    <li style="margin-top: 43px;">最低消费额</li>
                    <li>优惠券面值</li>
                    <li>优惠券的数量</li>
                    <li>优惠券生效时间</li>
                    <li>优惠券失效时间</li>
                </ul>
            </div>
            <div style="float: left;margin-left: 10px;">
                <ul id="modifyUlTd">
                    <li><input type="text" onkeyup="checkLength(this,20)" placeholder="最多10个字" id="modifyInputDName" name="dName"></li>
                    <li>
                        <textarea style="border: #00a4ac solid 0.2px;height: 50px;" onkeyup="checkLength(this,50)" placeholder="最多25字" id="modifyInputDDesc" name="dDesc"></textarea>
                    </li>
                    <li><input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')" id="modifyInputDMinConsum" name="dMinConsum"></li>
                    <li><input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')" id="modifyInputDValue" name="dValue"></li>
                    <li><input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" id="modifyInputDCount" name="dCount"></li>
                    <li><input type="datetime-local" id="modifyInputDStart" name="dStart"></li>
                    <li><input type="datetime-local" id="modifyInputDStop" name="dStop"></li>
                </ul>
            </div>
        </div>
        <div class="tipbtn" style="position: absolute;top:-90px;">
            <input name="" type="button"  class="sure" value="确定修改" id="modifyOK"/>&nbsp;
            <input name="" type="button"  class="cancel" value="取消" />
        </div>
    </div>
</form>
<!--删除选项框-->
<div class="tip" id="deleteDiv" style="height: 350px;">
    <div class="tiptop"><span>提示信息</span><a></a></div>
    <div class="tipinfo">
        <span><img src="/resources/hotel/images/ticon.png" /></span>
        <div class="tipright">
            <p>是否确认对信息的删除 ？</p>
            <cite>如果是请点击确定按钮 ，否则请点取消。</cite>
        </div>
    </div>
    <div class="tipbtn" style="margin: 100px;">
        <input name="" type="button"  class="sure" value="确定" id="deleteOK"/>&nbsp;
        <input name="" type="button"  class="cancel" value="取消" />
    </div>
</div>
<script type="text/javascript" src="/resources/hotel/js/util.js"></script>
<script type="text/javascript">
    $('.tablelist tbody tr:odd').addClass('odd');
</script>
<script type="text/javascript">
    //当前页数
    var nowPage;
    //总页数
    var countPage;

    //获取酒店所有订单信息
    getPage(1);//页面加载，获取订单第一页
    /**
     * 翻页
     * @param flip -1上一页 0跳转页 1下一页
     */
    function flip(flip) {
        switch (flip){
            case -1:
                if(nowPage-1>0){
                    nowPage=nowPage-1;
                    getPage(nowPage);//根据页码查询已完成订单
                }else {
                    alert("已经是第一页了");
                }
                break;
            case 0:
                var findPageNum = $("#finPageBox").val();
                if(findPageNum>0&&findPageNum<=countPage){
                    getPage(findPageNum);
                }else {
                    alert("页码错误!");
                }
                break;
            case 1:
                if((nowPage+1)*1<=countPage){
                    nowPage=nowPage+1;
                    getPage(nowPage);//根据页码查询已完成订单
                }else {
                    alert("已经是最后一页了");
                }
                break;
            default:
                alert("翻页请求错误！");
        }
    }

    function getPage(pageIndex,sequence,name,start,end) {
        //获取酒店已有优惠券信息
        $.ajax({
            type:"post",
            url:"http://localhost:8080/discountCouponController/selectAllDiscountCoupon",
            data:{"pageIndex":pageIndex},
            dataType:"json",
            success:function (data) {
                if(data.success){
                    nowPage = pageIndex;//设置当前页数
                    $("#nowPage").html(nowPage);
                    countPage = data.allPage;//设置总页数
                    $("#countPage").html(data.allPage);
                    $("#countData").html(data.allRecord);//设置总数据条数
                    var hadCouponBody = $("#hadCouponBody");
                    hadCouponBody.html("");//清空之前的数据
                    var tr = "";
                    for(var i=0;i<data.list.length;i++){
                        var coopon = data.list[i];
                        var dStatus="";
                        if(coopon.dStatus==0){
                            dStatus="未生效";
                        }else if(coopon.dStatus==1){
                            dStatus="生效中";
                        }else if(coopon.dStatus==2){
                            dStatus="已失效";
                        }else {
                            dStatus="错误状态";
                        }
                        var mUsername = coopon.dCreator.mUsername;
                        if(mUsername==undefined){
                            mUsername = "";
                        }
                        tr="<tr>"+
                            "<td>"+coopon.dName+"</td>"+
                            "<td>"+coopon.dDesc+"</td>"+
                            "<td>"+coopon.dValue+"</td>"+
                            "<td>"+mUsername+"</td>"+
                            "<td>"+timestampToTime(coopon.dCreateTime)+"</td>"+
                            "<td>"+timestampToTime(coopon.dStart)+"</td>"+
                            "<td>"+timestampToTime(coopon.dStop)+"</td>"+
                            "<td>"+coopon.dMinConsum+"</td>"+
                            "<td>"+dStatus+"</td>"+
                            "<td><a href='#' class='tablelink' onclick='modify("+coopon.dId+","+coopon.dStatus+")'>修改</a> <a href='#' class='tablelink' style='margin-left: 10px;' onclick='deleteButton("+coopon.dId+","+coopon.dStatus+")'>删除</a></td>"+
                            "</tr>";
                        hadCouponBody.append(tr);
                    }
                }else {
                    alert(data.errMsg);
                }
            },
            error:function () {
                alert("访问错误！");
            }
        });
    }

    //时间比较 小返回true
    function dateCompare(starttime,endtime) {
        var start = new Date(starttime.replace("-", "/").replace("-", "/"));
        var end = new Date(endtime.replace("-", "/").replace("-", "/"));
        if(start<=end){
            return true;
        }else {
            return false;
        }
    }
    //搜索的事件
    var sequence;
    var name;
    var start;
    var end
    $("#find").click(function () {
        sequence= $("#sequence").val();
        name= $("#name").val();
        start= $("#start").val();
        end= $("#end").val();
        if(start==null||start==""){
            start="1970-01-01";
        }
        if(end==null||end==""){
            end = getNowFormatDate();
        }

        if(dateCompare(start,end)){
            if(dateCompare(end,getNowFormatDate())){
                getPage(1,sequence,name,start,end);
            }else {
                alert("搜索错误！结束时间大于今日....");
            }
        }else {
            alert("搜索错误！开始时间大于结束时间....");
        }
    });

    //添加优惠券的事件
    $("#addButton").click(function () {
        $("#addDiv").show();
    });
    //点击确认添加
    $("#addOK").click(function () {
        var dName = $("#addInputDName").val();
        var dDesc = $("#addInputDDesc").val();
        var dMinConsum = $("#addInputDMinConsum").val();
        var dValue = $("#addInputDValue").val();
        var dCount = $("#addInputDCount").val();
        var dStart = $("#addInputDStart").val();
        var dStop = $("#addInputDStop").val();
        if(dName!=""&&dDesc!=""&&dMinConsum!=""&&dValue!=""&&dCount!=""&&dStart!=""&&dStop!=""){
            if(dateCompare(dStart,dStop)){
                if(dateCompare(getNowFormatDate(),dStart)){
                    var form = $("#addForm").serialize();
                    $.ajax({
                        url:"http://localhost:8080/discountCouponController/addDiscountCoupon",
                        type:"post",
                        data:form,
                        dataType:"json",
                        success:function (data) {
                            alert("请求成功")
                        },
                        error:function () {
                            alert("访问错误！");
                        }
                    });
                }else {
                    alert("生效日期必须大于当前时间..");
                }
            }else {
                alert("生效时间不能大于失效时间！")
            }
        }else {
            alert("添加失败，数据不完整!");
        }
    });
    //点击修改优惠券
    function modify(did,status){
        if(status==0){
            $("#modifyDiv").css("display","block");//显示修改框
            //根据id查询优惠券信息
            $.ajax({
                url:"http://localhost:8080/discountCouponController/queryDiscountCouponById",
                type:"get",
                dataType:"json",
                data:{"id":did},
                success:function (data) {
                    $("#modifyInputDName").val(data.dName);
                    $("#modifyInputDDesc").val(data.dDesc);
                    $("#modifyInputDMinConsum").val(data.dMinConsum);
                    $("#modifyInputDValue").val(data.dValue);
                    $("#modifyInputDCount").val(data.dCount);
                    $("#modifyInputDStart").val(timestampToTime(data.dStart));
                    $("#modifyInputDStop").val(timestampToTime(data.dStop));
                },
                error:function () {
                    alert("访问错误！");
                }
            });
        }else {
            alert("优惠券生效中或已失效，不能修改。。。。");
        }
        // 点击确认修改事件
        $("#modifyOK").click(function () {
            $.ajax({
                url:"http://localhost:8080/discountCouponController/updateDiscountCouponInfor",
                type:"post",
                dataType:"json",
                data:{
                    "id":did,
                    "dName":$("#modifyInputDName").val(),
                    "dDesc":$("#modifyInputDDesc").val(),
                    "dMinConsum":$("#modifyInputDMinConsum").val(),
                    "dValue":$("#modifyInputDValue").val(),
                    "dCount":$("#modifyInputDCount").val(),
                    "dStart":$("#modifyInputDStart").val(),
                    "dStop":$("#modifyInputDStop").val()
                },
                success:function (data) {
                    console.log(data);
                    alert("还在开发中.........");
                },
                error:function () {
                    alert("访问错误！");
                }
            });
        });
    }
    //点击删除显示删除div
    var dIdOfDelete;//要删除优惠券的id
    function deleteButton(dId,dStatus){
        dIdOfDelete = dId;
        if(dStatus==0){
            $("#deleteDiv").css("display","block");//显示修改框
        }else {
            alert("优惠券生效中或已失效，不能删除。。。。");
        }
    }
    //点击确认删除
    $("#deleteOK").click(function () {
        $.ajax({
            url:"http://localhost:8080/discountCouponController/deleteDiscountCouponById",
            type:"post",
            dataType:"json",
            data:{
                "id":dIdOfDelete
            },
            success:function (data) {
                if(data.success==true){
                    alert("删除成功");
                    window.location.reload();//刷新页面
                }else {
                    alert(data.message);//错误提示
                }
            },
            error:function () {
                alert("访问错误！");
            }
        });
    });
</script>
</body>
</html>
